/**
 * 
 */
package com.zhiteng.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhiteng.entity.CourseNode;
import com.zhiteng.entity.CourseTable;
import com.zhiteng.entity.CourseTime;
import com.zhiteng.entity.Teacher;
import com.zhiteng.entity.TimeTable;
import com.zhiteng.entity.WeekAndDayAndCourseNum;
import com.zhiteng.service.CourseTableService;
import com.zhiteng.service.CourseTimeService;
import com.zhiteng.service.RedisService;
import com.zhiteng.service.TimeTableService;
import com.zhiteng.utils.common.DateUtil;
import com.zhiteng.utils.common.ZTResult;
import com.zhiteng.vo.CourseTableVo;
import com.zhiteng.vo.TeacherVo;

import sun.misc.BASE64Encoder;

/**
 * <p>
 * Title: CourseTableController.java
 * </p>
 * <p>
 * Description: 课程表相关操作Controller
 * </p>
 * 
 * @author dragon
 * @date 2018年11月8日
 * @version 1.0
 */
@SuppressWarnings("restriction")
@Controller
@RequestMapping("/courseTable")
public class CourseTableController {

	@Autowired
	private CourseTableService courseTableService;
	@Autowired
	private CourseTimeService courseTimeService;
	@Autowired
	private TimeTableService timeTableService;

	// redis缓存测试
	/*
	 * @RequestMapping(value="/getTotal",produces="text/html;charset=UTF-8;")
	 * 
	 * @ResponseBody public String get() { long start = System.currentTimeMillis();
	 * String str = redisService.get("total"); System.out.println(str); long end =
	 * System.currentTimeMillis(); System.out.println("共耗时"+(end-start)+"毫秒!");
	 * return str; }
	 */

	/**
	 * 
	 * @Title: uploadTotalCourseTable
	 * @Description: 查询总课程表是否已经存在
	 * @param schoolId 学校id
	 * @param year     学年,如"2018-2019学年"
	 * @param term     学期,如"第一学期"
	 * @return ZTResult<Void>
	 */
	@RequestMapping("/queryTotalCourseTable")
	@ResponseBody
	public ZTResult<Object> queryTotalCourseTable(@RequestParam(name = "schoolId") String schoolId,
			@RequestParam(name = "year") String year, // 2018-2019学年
			@RequestParam(name = "term") String term) { // 第一学期

		if (StringUtils.isAnyBlank(schoolId, year, term) || year.length() != 11 || term.length() != 4) {
			return ZTResult.result(1, "上传失败，请检查参数");
		}
		int count = courseTableService.queryTotalCourseTable(schoolId, year, term);

		if (count > 0) {
			return ZTResult.result(1, "该学校" + year + term + "的总课表已经存在");
		}

		return ZTResult.result(0, "该学校" + year + term + "的总课表不存在，直接上传即可");
	}

	/**
	 * 
	 * @Title: uploadTotalCourseTable
	 * @Description:指定学校后上传excel形式的总课表，保存数据到数据库
	 * @param file     总课程表的Excel文件
	 * @param schoolId 学校id
	 * @param year     学年,如"2018-2019"
	 * @param term     学期,如"第一学期"
	 * @return ZTResult<Void>
	 */
	@SuppressWarnings({ "unchecked" })
	@RequestMapping("/uploadTotalCourseTable")
	@ResponseBody
	public synchronized ZTResult<Object> uploadTotalCourseTable(@RequestParam(name = "file") MultipartFile file,
			@RequestParam(name = "schoolId") String schoolId, @RequestParam(name = "year") String year, // 2018-2019
			@RequestParam(name = "term") String term) { // 第一学期

		if (StringUtils.isAnyBlank(schoolId, year, term) || year.length() != 9 || term.length() != 4) {
			return ZTResult.result(1, "上传失败，请检查参数");
		}

		Map<String, Object> resultMap = courseTableService.saveCourseTableAll(file, schoolId, year, term);

		//////////////////////////////////////// BOB：所有科目默认设置为一级科目 	/////////////////////////
		Set<String> TimeTable = timeTableService.queryAllTimeTable(schoolId);

		timeTableService.deleteTimeTable(schoolId);
		for (String subject : TimeTable) {
			TimeTable table = new TimeTable();
			table.setSchoolId(schoolId);
			table.setSubject(subject);
			table.setType(1);
			int i = timeTableService.saveTimeTable(table);
		}
		////////////////////////////// BOB/////////////////////////////////////////

		if ((int) resultMap.get("code") == 0) {
			return ZTResult.result(0, "课表上传成功");
		} else if ((int) resultMap.get("code") == 1) {
			List<String> unmatchedTeacherNames = (List<String>) resultMap.get("data");
			return ZTResult.result(0, "课表上传成功，但以下教师未匹配成功", unmatchedTeacherNames);
		} else {
			return ZTResult.result(1, "课表解析失败，请检查上传的excel文件！");
		}

	}
	
	/**
	 * 
	* @Title: queryUnmatchedTeacherNamesBySchoolId  
	* @Description: 根据学校id查询上传课程表过程中未匹配到的教师
	* @param schoolId
	* @return ZTResult<Object>   
	* @throws
	 */
	public ZTResult<Object> queryUnmatchedTeacherNamesBySchoolId(@RequestParam(name = "schoolId") String schoolId){
		List<String> unmatchedTeacherNames = courseTableService.queryUnmatchedTeacherNamesBySchoolId(schoolId);
		return ZTResult.result(0, "查询成功", unmatchedTeacherNames);
	}

	/**
	 * 
	 * @Title: saveUnmatchedTeacher
	 * @Description:手动上传未匹配到的教师信息（手动填写腾讯数据中的教师的手机号）
	 * @param schoolId    学校id
	 * @param cellphone   腾讯智慧校园里教师的手机号
	 * @param teacherName 总课程表中的教师的名称
	 * @return ZTResult<Object>
	 */
	@RequestMapping("/saveUnmatchedTeacher")
	@ResponseBody
	public ZTResult<Object> saveUnmatchedTeacher(TeacherVo teacherVo) {

		if (StringUtils.isAnyBlank(teacherVo.getSchoolId(), teacherVo.getTeacherName(), teacherVo.getCellphone())) {
			return ZTResult.result(1, "保存失败，请检查参数");
		}

		// 根据cellphone看腾讯智慧校园中是否有该教师，有的话返回教师id并保存到teacherVo中
		Teacher teacher = courseTableService.queryTeacherByTeacherVo(teacherVo);
		if (teacher == null) {
			return ZTResult.result(1, "该手机号对应的教师不存在");
		}

		courseTableService.saveUnmatchedTeacher(teacherVo);

		return ZTResult.result(0, "保存成功");
	}

	/**
	 * 
	 * @Title: queryCourseTableByVo
	 * @Description: 查询课程表
	 * @param school_id; // 学校ID
	 * @param type; // 课程表类型。1：班级表 2：教师表 3：总课表 4：总任课表
	 * @param teacherId; // 教师id
	 * @param grade_name; // 任课班级的年级名称
	 * @param class_name; // 任课班级名称
	 * @param year_name; // 学年名称
	 * @param term_name; // 学期名称
	 * @param weeks; // 周次（非必须，不传的话就根据当前日期查当前周次的）
	 * @return ZTResult<CourseTable> @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/queryCourseTable")
	@ResponseBody
	public ZTResult<CourseTable> queryCourseTable(CourseTableVo courseTableVo) {
		if (StringUtils.isBlank(courseTableVo.getSchool_id()) || courseTableVo.getType() == null) {
			return ZTResult.result(1, "查询失败，请检查参数，学校id和type为必须参数");
		}
		if (StringUtils.isBlank(courseTableVo.getWeeks() + "")) { // 根据当前日期判断周次
			try {
				String dateStr = DateUtil.Date2String(new Date());
				int weeks = DateUtil.getWeek(dateStr); // 当前周次
				courseTableVo.setWeeks(weeks);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		CourseTable table = null;
		try {
			table = courseTableService.queryCourseTable(courseTableVo);
			if (table == null)
				return ZTResult.result(1, "未查询到课表");
		} catch (Exception e) {
			e.printStackTrace();
			return ZTResult.result(1, "查询失败");
		}

		return ZTResult.result(0, "查询成功", table);
	}

	/**
	 * 
	 * @Title: queryCourseTimeTable @Description: 上传上课时间表前查询是否已经存在 @param
	 *         schoolId @return ZTResult<Object> @throws
	 */
	@RequestMapping("/queryCourseTimeTable")
	@ResponseBody
	public ZTResult<Object> queryCourseTimeTable(@RequestParam(name = "schoolId") String schoolId) {

		if (StringUtils.isBlank(schoolId)) {
			return ZTResult.result(1, "上传失败，请检查参数");
		}
		List<CourseTime> courseTimeList = courseTimeService.queryCourseTimeBySchoolId(schoolId);
		if (courseTimeList != null && !courseTimeList.isEmpty() && courseTimeList.size() > 0) {
			return ZTResult.result(1, "该学校的上课时间表已经存在");
		}
		return ZTResult.result(0, "该学校的上课时间表不存在，直接上传即可");
	}

	/**
	 * 
	 * @Title: uploadCourseTimeTable
	 * @Description: 上传上课时间表
	 * @param file     上课时间表Excel
	 * @param schoolId
	 * @return ZTResult<Object>
	 */
	@RequestMapping("/uploadCourseTimeTable")
	@ResponseBody
	public ZTResult<Object> uploadCourseTimeTable(@RequestParam(name = "file") MultipartFile file,
			@RequestParam(name = "schoolId") String schoolId) {

		if (StringUtils.isBlank(schoolId)) {
			return ZTResult.result(1, "上传失败，请检查参数");
		}

		try {
			// 先删除旧的时间表
			courseTimeService.deleteCourseTimeBySchoolId(schoolId);
			int count = courseTimeService.saveCourseTimeByExcel(file, schoolId);
			if (count == 0) {
				return ZTResult.result(0, "上传成功！");
			} else {
				return ZTResult.result(1, "上传失败，有" + count + "条记录插入失败");
			}
		} catch (Exception e) {
			return ZTResult.result(1, "上传失败，请检查excel文件");
		}

	}

	/**
	 * 
	 * @Title: downloadTimeTemplate @Description: 下载上课时间表模板 @throws
	 */
	@SuppressWarnings("restriction")
	@RequestMapping(value = "/downloadTimeTemplate")
	@ResponseBody
	public void downloadTimeTemplate(HttpServletRequest req, HttpServletResponse res, Model model) {
		// 要下载的文件名称
		String filename = "timeTemplate.xls";

		// 获得请求头中的User-Agent
		String agent = req.getHeader("User-Agent");
		// 根据不同浏览器进行不同的编码
		String filenameEncoder = "";
		try {
			if (agent.contains("MSIE")) {
				// IE浏览器
				filenameEncoder = URLEncoder.encode(filename, "utf-8");
				filenameEncoder = filenameEncoder.replace("+", " ");
			} else if (agent.contains("Firefox")) {
				// 火狐浏览器
				BASE64Encoder base64Encoder = new BASE64Encoder();
				filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
			} else {
				// 其它浏览器
				filenameEncoder = URLEncoder.encode(filename, "utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		// 告诉客户端(浏览器)要下载的这个文件的类型-----客户端通过文件的MIME类型去区分类型
		res.setContentType(req.getServletContext().getMimeType(filename));
		// 告诉客户端该文件不是直接解析,而是以附件形式打开(下载)
		res.setHeader("Content-Disposition", "attachment;filename=" + filenameEncoder);

		// 获取文件的绝对路径(注意写法)
		String realPath = req.getServletContext().getRealPath("/excelTemplate/timeTemplate.xls");
		// String realPath =
		// req.getSession().getServletContext().getRealPath("/excelTemplate/timeTemplate.xls");
		// 获得该文件的输入流
		InputStream in = null;
		try {
			in = new FileInputStream(new File(realPath));
			// 获得输出流---通过response获得的输出流 用于向客户端写内容
			OutputStream out = res.getOutputStream();

			// 文件拷贝的模板代码
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = in.read(buff)) > 0) {
				out.write(buff, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////

	@RequestMapping("/queryTeacherBySchoolId")
	@ResponseBody
	public ZTResult<List<CourseTable>> queryTeacherBySchoolId(@RequestParam("schoolId") String schoolId) {

		List<CourseTable> list = courseTableService.queryTeacherBySchoolId(schoolId, 2);

		if (list == null || list.isEmpty()) {
			return ZTResult.result(1, "根据学校的id查询老师的名字失败");
		}
		return ZTResult.result(0, "根据学校的id查询老师的名字成功", list);
	}

	/**
	 * 根据学校的id和星期,班级名,年级名,节次查询老师
	 * 
	 * @param schoolId
	 * @param          stuatus//查看是否已安排的老师,默认false没有安排
	 * @return
	 */
	@RequestMapping("/queryTeacherBySchoolIdAndRequestDayAndCourseNumAndClassNameAndGradeName")
	@ResponseBody
	public ZTResult<HashSet<Map<String, Object>>> queryTeacherBySchoolIdAndRequestDayAndCourseNum(
			@RequestParam("schoolId") String schoolId, @RequestParam("requestDay") String requestDay,
			@RequestParam("CourseNum") int CourseNum, @RequestParam("className") String className,
			@RequestParam("gradeName") String gradeName, @RequestParam("teacherId") String teacherId,
			@RequestParam("oldSubject") String oldSubject,
			@RequestParam(value = "status", required = false, defaultValue = "false") String stuatus,
			@RequestParam(value = "status2", required = false, defaultValue = "false") String stuatus2) {

		// 将日期转换为星期几,格式"2018-12-7"
		String week = DateUtil.dateToWeek2(requestDay);

		HashSet<CourseNode> list = null;
		if (stuatus.equals("false")) {
			if (stuatus2.equals("false")) {

				// 查询学校的那个班级的所有老师
				HashSet<CourseNode> lists = courseTableService.queryTeacherBySchoolIdAndClassName(schoolId, className);

				if (lists == null || lists.isEmpty()) {
					return ZTResult.result(1, "查询学校的那个班级的所有老师失败");
				}

				CourseNode CourseNode = courseTableService
						.queryTeacherBySchoolIdAndRequestDayAndCourseNumAndClassNameAndGradeName(schoolId, week,
								CourseNum, className, gradeName);

				if (CourseNode == null) {
					return ZTResult.result(1, "通过学校id,星期,节次,班级名,年级名查询CourseNode失败");
				}

				// 重复的老师
				HashSet<CourseNode> list2 = new HashSet<>();
				for (CourseNode courseNode : lists) {
					if (courseNode.getTeacher_id().equals(CourseNode.getTeacher_id())) {
						list2.add(courseNode);
					}
				}
				lists.removeAll(list2);
				list = lists;
			} else {
				// 查询同年级的老师
				HashSet<CourseNode> lists = courseTableService.queryTeacherBySchoolIdAndGradeName(schoolId, gradeName);
				if (lists == null || lists.isEmpty()) {
					return ZTResult.result(1, "查询同年级的老师失败");
				}
				//
				CourseNode CourseNode = courseTableService
						.queryTeacherBySchoolIdAndRequestDayAndCourseNumAndClassNameAndGradeName(schoolId, week,
								CourseNum, className, gradeName);
				if (CourseNode == null) {
					return ZTResult.result(1, "通过学校id,星期,节次,班级名,年级名查询CourseNode失败");
				}

				// 重复的老师
				HashSet<CourseNode> list2 = new HashSet<>();
				for (CourseNode courseNode : lists) {
					if (courseNode.getTeacher_id().equals(CourseNode.getTeacher_id())) {
						list2.add(courseNode);
					}
				}
				lists.removeAll(list2);
				list = lists;
			}
		} else {

			if (stuatus2.equals("false")) {
				// 查询学校的那个班级的所有老师
				HashSet<CourseNode> lists = courseTableService.queryTeacherBySchoolIdAndClassName(schoolId, className);

				if (lists == null || lists.isEmpty()) {
					return ZTResult.result(1, "查询学校的那个班级的所有老师失败");
				}

				CourseNode CourseNode = courseTableService
						.queryTeacherBySchoolIdAndRequestDayAndCourseNumAndClassNameAndGradeName(schoolId, week,
								CourseNum, className, gradeName);

				if (CourseNode == null) {
					return ZTResult.result(1, "通过学校id,星期,节次,班级名,年级名查询CourseNode失败");
				}

				// 重复的老师
				HashSet<CourseNode> list2 = new HashSet<>();
				for (CourseNode courseNode : lists) {
					if (courseNode.getTeacher_id().equals(CourseNode.getTeacher_id())) {
						list2.add(courseNode);
					}
				}
				lists.removeAll(list2);
				list = lists;

			} else {
				// 查询同年级的老师
				HashSet<CourseNode> lists = courseTableService.queryTeacherBySchoolIdAndGradeName(schoolId, gradeName);
				if (lists == null || lists.isEmpty()) {
					return ZTResult.result(1, "查询同年级的老师失败");
				}

				// 将日期转换为星期几,格式"2018-12-7"
//				String week = DateUtil.dateToWeek2(requestDay);
				// 查询当天安排的老师
				List<CourseNode> CourseNodeList = courseTableService
						.queryTeacherBySchoolIdAndRequestDayAndCourseNum(schoolId, week, CourseNum);

				if (CourseNodeList == null || CourseNodeList.isEmpty()) {
					return ZTResult.result(1, "查询学校的那个班级的所有老师失败");
				}

				// 重复的老师
				HashSet<CourseNode> list2 = new HashSet<>();
				for (CourseNode CourseNode : CourseNodeList) {
					for (CourseNode courseTable : lists) {
						if (CourseNode.getTeacher_id().equals(courseTable.getTeacher_id())) {
							list2.add(courseTable);
						}
					}
				}
				lists.removeAll(list2);
				list = lists;
			}
		}
		if (list.isEmpty() || list.size() == 0) {
			return ZTResult.result(1, "失败");
		}

		HashSet<Map<String, Object>> teacherName = new HashSet<>();

		List<TimeTable> list2 = timeTableService.queryTimeTableBySubject(oldSubject, schoolId);
		if (list2 == null || list2.isEmpty() || list2.get(0).getType() == 1) {
			Map<String, Object> map = new HashMap<>();
			for (CourseNode courseTable : list) {
				map.put(courseTable.getTeacher_id(), courseTable.getTeacher_name());
			}
			teacherName.add(map);
		}

		if (list2 == null || list2.isEmpty() || list2.get(0).getType() == 2) {
			Map<String, Object> map = new HashMap<>();
			for (CourseNode courseTable : list) {
				List<TimeTable> list3 = timeTableService.queryTimeTableBySubject(courseTable.getCourse_name(),
						schoolId);
				if (list3 == null || list3.isEmpty()) {
					return ZTResult.result(1, "有一门课没有设置,请先去设置");
				}
				if (list3.get(0).getType() == 2) {
					map.put(courseTable.getTeacher_id(), courseTable.getTeacher_name());
				}
			}
			teacherName.add(map);
		}
		return ZTResult.result(0, "成功", teacherName);
	}

	/**
	 * 根据老师的名查课程名
	 * 
	 * @param teacherName
	 * @return
	 */
	@RequestMapping("/queryKCByTeacherName")
	@ResponseBody
	public ZTResult<Set<String>> queryKCByTeacherName(
			@RequestParam(value = "teacherName", required = false) String teacherName,
			@RequestParam("schoolId") String schoolId) {

		List<CourseNode> CourseNodeList = null;
		if (teacherName == null || teacherName.isEmpty()) {
			CourseNodeList = courseTableService.queryKCBySchoolId(schoolId);
		} else {
			CourseNodeList = courseTableService.queryKCByTeacherNameAndSchoolId(schoolId, teacherName);
		}
		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
			return ZTResult.result(1, "根据老师的名查课程名失败");
		}

		Set<String> KCS = new HashSet<String>();

		for (CourseNode courseNode : CourseNodeList) {
			KCS.add(courseNode.getCourse_name());
		}
		return ZTResult.result(0, "成功", KCS);
	}

	/**
	 * 根据老师的id和学校的id查课程名
	 * 
	 * @param teacherName
	 * @return
	 */
	@RequestMapping("/queryKCByTeacherIdAndSchoolID")
	@ResponseBody
	public ZTResult<Set<String>> queryKCByTeacherIdAndSchoolID(
			@RequestParam(value = "TeacherId", required = false) String TeacherId,
			@RequestParam("schoolId") String schoolId) {

		List<CourseNode> CourseNodeList = null;
		if (TeacherId == null || TeacherId.isEmpty()) {
			CourseNodeList = courseTableService.queryKCBySchoolId(schoolId);
		} else {
			CourseNodeList = courseTableService.queryKCByTeacherIdAndSchoolID(TeacherId, schoolId);
		}
		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
			return ZTResult.result(1, "根据老师的名查课程名失败");
		}

		Set<String> KCS = new HashSet<String>();

		for (CourseNode courseNode : CourseNodeList) {
			KCS.add(courseNode.getCourse_name());
		}
		return ZTResult.result(0, "成功", KCS);
	}

	/**
	 * 查询时间根据老师的名字和课程
	 * 
	 * @return
	 */
	@RequestMapping("/queryTimeByTeacherNameAndKC")
	@ResponseBody
	public ZTResult<ArrayList<WeekAndDayAndCourseNum>> queryTimeByTeacherNameAndKC(
			@RequestParam(value = "teacherName") String teacherName, @RequestParam("schoolId") String schoolId,
			@RequestParam("KC") String KC) {

		List<CourseNode> CourseNodeList = courseTableService.queryTimeByTeacherNameAndKC(schoolId, teacherName, KC);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String dateFrom = format.format(new Date());

		Calendar c = Calendar.getInstance();
		// 过去一月
		c.setTime(new Date());
		c.add(Calendar.MONTH, +1);
		Date m = c.getTime();
		String dateEnd = format.format(m);

		Set<WeekAndDayAndCourseNum> list2 = new HashSet<WeekAndDayAndCourseNum>();

		for (CourseNode courseNode : CourseNodeList) {
//			List<String> list = DateUtil.getDays(yearStr, map.get(courseNode.getWeek()));// 得到所有的时间
			String[] list = DateUtil.getDates(dateFrom, dateEnd, courseNode.getWeek());

			List<Integer> Course_nums = courseTableService.queryCourseNumByTeacherNameAndKCAndWeek(schoolId,
					teacherName, KC, courseNode.getWeek());
			for (Integer Course_num : Course_nums) {
				for (String day : list) {
					WeekAndDayAndCourseNum laobao = new WeekAndDayAndCourseNum();
					laobao.setWeek(courseNode.getWeek());
					laobao.setDay(day);
					laobao.setCourse_num(Course_num);
					list2.add(laobao);
				}
			}
		}
		ArrayList<WeekAndDayAndCourseNum> list3 = new ArrayList<>(list2);
		Collections.sort(list3);
		return ZTResult.result(0, "成功", list3);
	}

//	@RequestMapping("/queryClassNameByTeacherNameAndRequestDay")
//	@ResponseBody
//	public ZTResult<List<CourseNode>> queryClassNameByTeacherNameAndRequestDayAnd(
//			@RequestParam(value = "teacherId") String teacherId, @RequestParam("schoolId") String schoolId,
//			@RequestParam("requestDay") String requestDay) {
//
//		// 将日期转换为星期几,格式"2018-12-7"
//		String week = DateUtil.dateToWeek2(requestDay);
//
//		List<CourseNode> CourseNodeList = courseTableService.queryClassNameByTeacherNameAndKCAndRequestDay(schoolId,
//				teacherId, week);
//		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
//			return ZTResult.result(1, "根据老师的名和课程名查询班级失败,查询的为空");
//		}
////		List<String> classNames = new ArrayList<>();
////		for (CourseNode courseNode : CourseNodeList) {
////			classNames.add(courseNode.getClass_name());
////		}
//		return ZTResult.result(0, "成功", CourseNodeList);
//	}

	// ***********************************************************************************

	/**
	 * 根据老师的id和学校的id和日期查询班级
	 * 
	 * @param teacherName
	 * @param schoolId
	 * @param KC
	 * @return
	 */
	@RequestMapping("/queryClassNameByTeacherNameAndRequestDay")
	@ResponseBody
	public ZTResult<Set<CourseNode>> queryClassNameByTeacherNameAndRequestDay(
			@RequestParam(value = "teacherId") String teacherId, @RequestParam("schoolId") String schoolId,
			@RequestParam("requestDay") String requestDay) {

		// 将日期转换为星期几,格式"2018-12-7"
		String week = DateUtil.dateToWeek2(requestDay);

		List<CourseNode> CourseNodeList = courseTableService.queryClassNameByTeacherNameAndKCAndRequestDay(schoolId,
				teacherId, week);
		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
			return ZTResult.result(1, "根据老师的名和课程名查询班级失败,查询的为空");
		}
//		List<String> classNames = new ArrayList<>();
//		for (CourseNode courseNode : CourseNodeList) {
//			classNames.add(courseNode.getClass_name());
//		}

		Set<CourseNode> set = new TreeSet<CourseNode>(new Comparator<CourseNode>() {
			@Override
			public int compare(CourseNode a, CourseNode b) {
				// 字符串则按照asicc码升序排列
				return a.getClass_name().compareTo(b.getClass_name());
			}
		});

		set.addAll(CourseNodeList);

		return ZTResult.result(0, "成功", set);
	}

	/**
	 * 根据老师的id和课程名和日期和班级名,查询节次
	 * 
	 * @param teacherId
	 * @param schoolId
	 * @param KC
	 * @param requestDay
	 * @param className
	 * @return
	 */
	@RequestMapping("/queryCourseNumByTeacherNameAndRequestDayAndClassName")
	@ResponseBody
	public ZTResult<List<CourseNode>> queryCourseNumByTeacherNameAndRequestDayAndClassName(
			@RequestParam(value = "teacherId") String teacherId, @RequestParam("schoolId") String schoolId,
			@RequestParam("requestDay") String requestDay, @RequestParam("className") String className) {
		// 将日期转换为星期几,格式"2018-12-7"
		String week = DateUtil.dateToWeek2(requestDay);
		List<CourseNode> CourseNodeList = courseTableService
				.queryCourseNumByTeacherNameAndKCAndRequestDayAndClassName(teacherId, schoolId, week, className);
		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
			return ZTResult.result(1, "根据老师的id和课程名和日期和班级名,查询节次失败,查询的为空");
		}
//		List<Integer> list = new ArrayList<>();
//		for (CourseNode courseNode : CourseNodeList) {
//			list.add(courseNode.getCourse_num());
//		}
		return ZTResult.result(0, "成功", CourseNodeList);
	}

	/**
	 * 根据老师的id和日期和班级名和节次查询课程名
	 * 
	 * @return
	 */
	@RequestMapping("/queryKCByTeacherIdAndSchoolIDAndRequestDayAndClassNameAndCourseNum")
	@ResponseBody
	public ZTResult<List<CourseNode>> queryKCByTeacherIdAndSchoolIDAndRequestDayAndClassNameAndCourseNum(
			@RequestParam(value = "teacherId") String teacherId, @RequestParam("schoolId") String schoolId,
			@RequestParam("requestDay") String requestDay, @RequestParam("className") String className,
			@RequestParam("CourseNum") int CourseNum) {
		// 将日期转换为星期几,格式"2018-12-4"
		String week = DateUtil.dateToWeek2(requestDay);
		List<CourseNode> CourseNodeList = courseTableService
				.queryKCByTeacherIdAndSchoolIDAndRequestDayAndClassNameAndCourseNum(teacherId, schoolId, week,
						className, CourseNum);

		if (CourseNodeList.isEmpty() || CourseNodeList.size() == 0) {
			return ZTResult.result(1, "根据老师的id和日期和班级名和节次查询课程名,查询为空");
		}
//		Set<String> set = new HashSet<>();
//		for (CourseNode CourseNode : CourseNodeList) {
//			set.add(CourseNode.getCourse_name());
//		}
		return ZTResult.result(0, "成功", CourseNodeList);
	}

	// ****************************************************************

//	public ZTResult<Void> 

}